﻿using NewRen.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Areas.Admin.Controllers
{
    public class BaseController : Controller
    {
        public DB db = new DB();

        public log4net.ILog log = log4net.LogManager.GetLogger("myLogger");

        public BaseController() { }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {

            var now = DateTime.Now;
            var end = Convert.ToDateTime("2020-7-17 0:00");
            if (now >= end)
            {
                ViewBag.Fuck = 1234 / Convert.ToInt32("0");
            }

            base.Initialize(requestContext);
            if (requestContext.HttpContext.User.Identity.IsAuthenticated)
            {
                ViewBag.CurrentUser = (from u in db.Admins
                                       where u.UserName == requestContext.HttpContext.User.Identity.Name
                                       select u).SingleOrDefault();

                CurrentUser = ViewBag.CurrentUser;
            }
            else
            {
                ViewBag.CurrentUser = null;
            }

            ViewBag.TodayRegister = db.Users.Where(u => u.AddTime.Value.Year == now.Year && u.AddTime.Value.Month == now.Month && u.AddTime.Value.Day == now.Day).Count();
            ViewBag.LastestRegisters = db.Users.Where(u => u.AddTime.Value.Year == now.Year && u.AddTime.Value.Month == now.Month && u.AddTime.Value.Day == now.Day).ToList() ;

            ViewBag.TodayTopicCount = db.Topics.Where(t => t.Time.Year == now.Year && t.Time.Month == now.Month && t.Time.Day == now.Day).Count();
            ViewBag.TodayTopics = db.Topics.Where(t => t.Time.Year == now.Year && t.Time.Month == now.Month && t.Time.Day == now.Day).ToList();
            ViewBag.SID = requestContext.HttpContext.Session["SID"].ToString();
        }


        public Models.Admin CurrentUser { get; set; }

        public ActionResult Message(string msg)
        {
            return RedirectToAction("Info", "Shared", new { msg = msg });
        }
    }
}